package com.ldc.common.utils;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

import static com.ldc.common.utils.ConstantValue.*;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/17
 */
public class RequestUtils {
    public RequestUtils() {
        throw new IllegalStateException("非法创建");
    }

    public static String getUserId(HttpServletRequest request) {
        return JwtUtils.getUserId(getToken(request));
    }

    public static String getUserName(HttpServletRequest request) {
        return JwtUtils.getUserName(getToken(request));
    }

    public static String getToken(HttpServletRequest request) {
        return getParam(request, ARG_TOKEN);
    }

    public static String getParam(HttpServletRequest request, String key) {
        String token = request.getParameter(key);
        if (StringUtils.hasLength(token)) {
            return token;
        }
        return request.getHeader(key);
    }
}
